<?php

namespace Backend\Modules\Video\Controllers;

use Library\Constant\Constant;
use \Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Mvc\Model\Resultset;
use Library\Pagination\Pager as Pager;
use Backend\Modules\Video\Models\VideoFile;
use Backend\Modules\Video\Forms\FormCreateVideoFile;

class VideoFileController extends \BackendController
{
    public $urlVideoFile = 'video/file';

    public function indexAction()
    {
        $currentPage = abs($this->request->getQuery('page', 'int', 1));
        $videoFile = VideoFile::find();
        $data = array();

        if ($currentPage == 0) {
            $currentPage = 1;
        }
        $pager = new Pager(
            new Paginator(array(
                'data'  => $videoFile,
                'limit' => Constant::ITEM_PER_PAGE_20 ,
                'page'  => $currentPage,
            )),
            array(
                'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                // Range window will be 5 pages
                'rangeLength' => 5,
                // Just a string with URL mask
                'urlMask'     => '?page={%page_number}',
            )
        );

        $this->view->pager = $pager;
    }

    public function createAction()
    {
        $form = new FormCreateVideoFile();
        $form->setAction($this->getBackendUrl() . '/' . $this->urlVideoFile . '/create');

        if($this->request->isPost())
        {
            if($form->isValid($this->request->getPost()) != false)
            {
                $videoFile = new VideoFile();
                $dataPost = $this->request->getPost();
                $videoFile->assign(array(
                    'name' => $dataPost['name'],
                    'source_server' => $dataPost['source_server'],
                    'link_source_server' => $dataPost['link_source_server'],
                    'quanlity' => $this->request->getPost("quanlity" , "int"),
                    'quanlity_description' => $dataPost['quanlity_description'],
                    'file_path' => $dataPost['file_path'],
                    'file_extension' => $dataPost['file_extension'],
                    'protocol' => $dataPost['protocol'],
                    'agent' => $dataPost['agent'],
                    'status' => $dataPost['status']
                ));
                if($videoFile->save())
                {
                    return $this->response->redirect($this->getBackendUrl() . '/' . $this->urlVideoFile);
                }
                $this->flash->error($videoFile->getMessages());
            }
        }
        $this->view->form = $form;
    }

    public function editAction($id)
    {
        $videoFile = VideoFile::findFirstById($id);
        if(!$videoFile)
        {
            $this->flash->error("Video file was not found");
            return $this->dispatcher->forward(array(
                'action' => 'index'
            ));
        }

        $form = new FormCreateVideoFile($videoFile , array('edit' => true));
        $form->setAction($this->getBackendUrl() . '/' . $this->urlVideoFile . '/edit/' . $id);

        if($this->request->getPost())
        {
            if($form->isValid($this->request->getPost()) != false)
            {
                $dataPost = $this->request->getPost();
                $videoFile->assign(array(
                    'name' => $dataPost['name'],
                    'source_server' => $dataPost['source_server'],
                    'link_source_server' => $dataPost['link_source_server'],
                    'quanlity' => $dataPost['quanlity'] ? $dataPost['quanlity'] : 0,
                    'quanlity_description' => $dataPost['quanlity_description'],
                    'file_path' => $dataPost['file_path'],
                    'file_extension' => $dataPost['file_extension'],
                    'protocol' => $dataPost['protocol'],
                    'agent' => $dataPost['agent'],
                    'status' => $dataPost['status']
                ));
                if($videoFile->save())
                {
                    return $this->response->redirect($this->getBackendUrl() . '/' . $this->urlVideoFile);
                }
                $this->flash->error($videoFile->getMessages());
            }
        }
        $this->view->form = $form;
    }

    /*
     * Delete video group
     */

    public function deleteAction($id)
    {
        $videoFile = VideoFile::findFirstById($id);
        if(!$videoFile)
        {
            $this->flash->error("Video was not found");
            return $this->dispatcher->forward(array(
                'action' => 'index'
            ));
        }
        if(!$videoFile->delete())
        {
            $this->flash->error($videoFile->getMessages());
        }
        else
        {
            $this->flash->success("Video was deleted");
        }

        return $this->dispatcher->forward(array(
            'action' => 'index'
        ));
    }
}